package cn.wyl.user.controller;

import cn.wyl.basic.exception.BusinessException;
import cn.wyl.basic.util.AxiousResult;
import cn.wyl.system.annotation.PreAuthorize;
import cn.wyl.user.domain.Logininfo;
import cn.wyl.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.Map;

/**
 * 前端传入：this.$http.post("/login/account",this.ruleForm2)
 *          ruleForm2: {
 *           username: '',
 *           phone:'',
 *           password: ''
 *           type:1
 *         }
 * 要求后端返回 AxiousResult
 *
 * 登录接口，处理前端员工登录请求的
 *
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/account")
    public AxiousResult accountLogin(@RequestBody Map loginMap){
        try {
            //登录成功要返回tokenLogininfoMap登录信息
            Map<String, Object> tokenLogininfoMap=logininfoService.accountLogin(loginMap);
            return new AxiousResult(true,"登录成功",tokenLogininfoMap);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AxiousResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @GetMapping("/wechat/{code}")
    public AxiousResult wechatLogin(@PathVariable("code") String code){
        try {
            //通过logininfoService调用微信登录方法，传入授权码
          return  logininfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AxiousResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"系统异常，请稍后重试!!!");
        }

    }

    @PostMapping("/wechat/binder")
    public AxiousResult wechatBinder(@RequestBody Map<String,Object> paramsMap){
        try {
            return  logininfoService.wechatBinder(paramsMap);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AxiousResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"系统异常，请稍后重试!!!");
        }
    }

    @GetMapping("/loginout/{token}")
    public AxiousResult loginout(@PathVariable("token") String token){
        try {
            //通过logininfoService调用注销登录，传入token
            return  logininfoService.loginout(token);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AxiousResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"系统异常，请稍后重试!!!");
        }

    }

}
